import sys
sys.path.append("..")
from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
from common.entity_data import EntityData
import glob
import jieba
import re
import threading
import hashlib

'''
品牌
'''

class PinpaiData:
    def __init__(self):
        self.entity = EntityData()
        self.url_map = {}
        pass

    def get_detail(self, pinpai_name, pinpai_url):
        print(pinpai_url)
        bs4_data = MyHttp.bs4_gb2312_data(pinpai_url)
        name = pinpai_name.split("/")[0]
        do_tag = False
        name_set = ""
        for tr in bs4_data.find(name="table", attrs={'class':'t12'}).find_all("tr"):
            if tr.get("class", "") == "t9 white":
                if do_tag:
                    break
                else:
                    do_tag = True
            for td in tr.find_all("td"):
                for a in td.find_all("a"):
                    if a.find("img"):
                        continue
                    if a.get("href","") != "":
                        href = a.attrs["href"]
                        text = a.text
                        text_v1 = text.replace(".","").replace("-","").replace("&","").replace(" ","").replace("·","").replace("'","")
                        for i in range(97, 123):
                            text_v1 = text_v1.replace(chr(i),"")
                        for i in range(65, 91):
                            text_v1 = text_v1.replace(chr(i),"")
                        text_v3 = text_v1
                        if text_v3 == "":
                            text_v3 = text
                        name_set += "|" + text_v3
        #print(name + ":" + name_set)
        self.entity.add_entity_data(name + "品牌", "", 100, 1000004, name, "", name_set)
        self.entity.update_entity_data(name + "品牌", "", 100, 1000004, name, "", name_set)
    
    def get_data(self):
        url = "http://10.ip138.com/"
        bs4_data = MyHttp.bs4_gb2312_data(url)
        for tr in bs4_data.find(name="table", attrs={'class':'t12'}).find_all("tr"):
            tds = tr.find_all("td")
            if len(tds) != 5:
                continue
            for td in tds:
                if td.find("a"):
                    pinpai_url = td.find("a").attrs["href"]
                    pinpai_name = td.find("a").text
                    self.url_map[pinpai_name] = "http://10.ip138.com/" + pinpai_url
        for pinpai_name,pinpai_url in self.url_map.items():
            try:
                self.get_detail(pinpai_name,pinpai_url)
            except Exception as e:
                print(e)
                pass


        '''
            for valid_data in ul.find_all(name="div", attrs={'class':'w-zx-sfcs4'}):
                #print(str(valid_data))
                title_data = valid_data.find("a").text.replace(u" ","").replace("\n","")
                for key,value in self.diqu.items():
                    if re.search(key, title_data):
                        if value in self.diqu_fayuan.keys():
                            self.diqu_fayuan[value] = self.diqu_fayuan[value] + "|" + title_data
                        else:
                            self.diqu_fayuan[value] = title_data
                        self.diqu_fayuan[u"中国大陆地区法院"] = self.diqu_fayuan[u"中国大陆地区法院"] + "|" + title_data
                print(title_data)
                #self.macro.add_macro_data(date_data, 100, title_data, url_data, "中国财政部")
        '''

    '''
    def get_data(self):
        
        for key,value in self.diqu_fayuan.items():
            #print(key + ":" + value)
            self.entity.add_entity_data(key, "", 101, 1010001, "", "", value)
            self.entity.update_entity_data(key, "", 101, 1010001, "", "", value)
    '''

def pinpai_api():
    d = PinpaiData()
    d.get_data()

if __name__ == '__main__':
    pinpai_api()
